package com.forestar.mapControl.utils.mapConvert;

import android.graphics.Rect;

import com.forestar.mapControl.mappool.MapCachePool2;
import com.forestar.mapControl.mappool.MzTileBean;
import com.forestar.mapControl.mapview.CacheCanvas;
import com.forestar.mapControl.utils.LogD;
import com.forestar.mapControl.utils.bitmapcache.CacheableBitmapDrawable;
import com.forestar.mapControl.utils.mapConvert.geometry.Envelope;
import com.forestar.mapControl.utils.mapConvert.geometry.GeoPoint;
import com.forestar.mapControl.utils.tile.TilesIndexBound;

import java.util.ArrayList;
import java.util.List;

import static com.forestar.mapControl.utils.mapConvert.convert.ConvertHelper2.TIANDITU_SAT_KEY2;

/**
 * created by  xzk
 * 2020/8/7   10:37
 * 说明:
 */
public class ConvertHelper {


//    ------------------      把任务提出来做任务梳理         --------------------
//

//        //遍历绘制
//        for(CacheableBitmapDrawable forShow: tempList){
//            //todo
////                if(!MapCachePool2.getInstance().getAllScreenTileHasLoad().contains(forShow.getUrl())){
//            forTexture.drawBitmap(forShow.getBitmap(),null,forShow.getBounds(),tempPaint);
////                    LogD.d("显示到屏幕的瓦片:"+forShow.getUrl());
////                    MapCachePool2.getInstance().getAllScreenTileHasLoad().add(forShow.getUrl());
////                }
//        }










//    ----------------------  子线程分析加载地图任务 --------------------------
//private Runnable sumTileAreaTask = new Runnable() {
//    @Override
//    public void run() {
//        int level = tileSchema.getBestLevelByResolution(transformNew.getResolutionInMapUnit(), screenPPI);
//        if (level == -1) {
//            return;
//        }
//        Envelope envlope = transformNew.getViewBound();
//        TilesIndexBound tilesBound = tileSchema.getTilesIndexBound(level, envlope.getXMin(), envlope.getXMax(), envlope.getYMin(), envlope.getYMax());
//        LogD.d("本次绘制的区域:"+tilesBound.toString()+"----");
//        if (tilesBound.isEmpty()) {
//            return ;
//        }
//        CacheCanvas cacheCanvas1 = cacheCanvasList.get(1);
////            temppaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
////            cacheCanvas1.getCanvas().drawPaint(temppaint);
////            temppaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
//
//        //看看偏移的多不多
//        List<CacheableBitmapDrawable> tempList = new ArrayList<>();
////            //总共1层
//        long start = System.currentTimeMillis();
//        long endForCanvasMill=0;
//
//        //新创建的似乎没什么用，，，直接内容都不显示了。
//        int xMore = 2;
//        int yMore = 2;
//        //计算中心点
////            int middleY=(tilesBound.maxTileY-tilesBound.minTileY)/2+tilesBound.minTileY;
////            int middleX=(tilesBound.maxTileX-tilesBound.minTileX)/2+tilesBound.minTileX;
//
//        //先清除下上一屏需要的数据
//        MapCachePool2.getInstance().getNoScreenTile().clear();
//        //这里gc一下
//        MapCachePool2.getInstance().removeAllTask();
//
//        //在屏幕边缘的区域
//        for (int y = tilesBound.minTileY ; y <= tilesBound.maxTileY ; y++) {
//            for (int x = tilesBound.minTileX ; x <= tilesBound.maxTileX ; x++) {
//                final MzTileBean tile = new MzTileBean(TIANDITU_SAT_KEY2, tilesBound.level, x, y);
//                drawTile(tile);
//                MapCachePool2.getInstance().getNoScreenTile().add(tile.getCacheKey());
//                CacheableBitmapDrawable forShow = MapCachePool2.getInstance().getBitmapForShow(tile);
//                //在循环中就直接绘制了
//                if(null != forShow ){
//                    tempList.add(forShow);
//                }
//            }
//        }
//
//        //放入屏幕外围的预加载任务
//        for (int y = tilesBound.minTileY-yMore; y <= tilesBound.maxTileY+yMore; y++) {
//            for (int x = tilesBound.minTileX-xMore; x <= tilesBound.maxTileX+xMore; x++) {
//                //接入外围区域
//                if(y>tilesBound.minTileY && y<tilesBound.maxTileY && x > tilesBound.minTileX && x < tilesBound.maxTileX ){
//                    continue;
//                }
//                final MzTileBean tile = new MzTileBean(TIANDITU_SAT_KEY2, tilesBound.level, x, y);
//                drawTile(tile);
////                    //外围区域不绘制，只预加载
//                MapCachePool2.getInstance().putForRequestTaskIfNeed(tile);
//            }
//        }
//
//        //遍历绘制
//        for(CacheableBitmapDrawable forShow: tempList){
//            //todo
////                if(!MapCachePool2.getInstance().getAllScreenTileHasLoad().contains(forShow.getUrl())){
//            cacheCanvas1.getCanvas().drawBitmap(forShow.getBitmap(),null,forShow.getBounds(),tempPaint);
////                    LogD.d("显示到屏幕的瓦片:"+forShow.getUrl());
////                    MapCachePool2.getInstance().getAllScreenTileHasLoad().add(forShow.getUrl());
////                }
//        }
//
//        drawForCache();
//
//        long endDraw = System.currentTimeMillis();
//        LogD.d("单次拉取瓦片:效率：" + (endDraw - start) + ".");
//        hasTaskNum.decrementAndGet();
//        //添加一个数字  开始加载
//        for (int i = 0; i < 10; i++) {
//            loadTilePool.execute(new MapConvert2.LoadTileTask());
//        }
////        //一次性绘制到界面上的效率太低了，每次 45-48之间，甚至更高（这里是必须优化下来的）
//    }
//};

//    --------------------------------------



//    ---------------------      偏移画布 随手指移动更换显示区域         ----------------------

//    GeoPoint geoPoint = cacheTrans.screenPoint2MapPoint(0, 0);
//    oriPointF = mapViewTransform.mapPoint2ScreenPoint(geoPoint);
//    //这里是 是对图片进行裁截，若是空null则显示整个图片
//    Rect srcCacheRect = new Rect();
//    srcCacheRect.left = 0;
//    srcCacheRect.top = 0;
//    srcCacheRect.right = (int) (cacheTrans.getScreenCenterX() * 2);
//    srcCacheRect.bottom = (int) (cacheTrans.getScreenCenterY() * 2);
//    resolutionScale = (cacheTrans.getResolutionInMapUnit() / cacheTrans.getResolutionInMapUnit());
//    width = (int) (cacheTrans.getScreenCenterX() * 2 * resolutionScale);
//    height = (int) (cacheTrans.getScreenCenterY() * 2 * resolutionScale);
//
//    srcCacheRect.left=srcCacheRect.left+((int)cacheTrans.getDistanceX());
//    srcCacheRect.right=srcCacheRect.right+((int)cacheTrans.getDistanceX());
//    srcCacheRect.top=srcCacheRect.top+((int)cacheTrans.getDistanceY());
//    srcCacheRect.bottom=srcCacheRect.bottom+((int)cacheTrans.getDistanceY());
////                LogD.d("绘制区域srcCacheRect："+srcCacheRect);
//
//    //是图片在Canvas画布中显示的区域
//    Rect dstCanvasRect = new Rect();
//    dstCanvasRect.left = (int) oriPointF.x;
//    dstCanvasRect.top = (int) oriPointF.y;
//    dstCanvasRect.right = (int) oriPointF.x + width;
//    dstCanvasRect.bottom = (int) oriPointF.y + height;
//
//    dstCanvasRect.left=dstCanvasRect.left+((int)cacheTrans.getDistanceX());
//    dstCanvasRect.right=dstCanvasRect.right+((int)cacheTrans.getDistanceX());
//    dstCanvasRect.top=dstCanvasRect.top+((int)cacheTrans.getDistanceY());
//    dstCanvasRect.bottom=dstCanvasRect.bottom+((int)cacheTrans.getDistanceY());
////                LogD.d("绘制区域dstCanvasRect："+dstCanvasRect);
////                testPaint.setColor(Color.BLUE);
////                canvasCache.drawRect(srcCacheRect,testPaint);
//
////                testPaint.setColor(Color.DKGRAY);
////                canvasCache.drawRect(dstCanvasRect,testPaint);
//
////                canvasCache.translate(dstCanvasRect.left,dstCanvasRect.top);
//                canvasCache.drawBitmap(cacheCanvas.getBitmap(), srcCacheRect, dstCanvasRect, temppaint);
//    ---------------------      偏移画布 随手指移动更换显示区域         ----------------------







//   ---------------------------        屏幕分成区域，分割加载  配合LinkedHashMap  最近优先加载             -----------------------------
//            for (int y = centerTop ; y <= centerBottom ; y++) {
//        for (int x = centerLeft ; x <= centerRight ; x++) {
//            final MzTileBean tile = new MzTileBean(TIANDITU_SAT_KEY2, tilesBound.level, x, y);
//            drawTile(tile,cacheCanvas0);
//            MapCachePool2.getInstance().getNoScreenTile().add(tile.getCacheKey());
//            CacheableBitmapDrawable forShow = MapCachePool2.getInstance().getBitmapForShow(tile);
//            //在循环中就直接绘制了
//            if(null != forShow ){
//                tempList.add(forShow);
//            }
//        }
//    }
//
//    //在屏幕边缘的区域
//            for (int y = tilesBound.minTileY ; y <= tilesBound.maxTileY ; y++) {
//        for (int x = tilesBound.minTileX ; x <= tilesBound.maxTileX ; x++) {
//            if(y>centerTop && y<centerBottom && x > centerLeft && x < centerRight ){
//                continue;
//            }
//            final MzTileBean tile = new MzTileBean(TIANDITU_SAT_KEY2, tilesBound.level, x, y);
//            drawTile(tile,cacheCanvas0);
//            MapCachePool2.getInstance().getNoScreenTile().add(tile.getCacheKey());
//            CacheableBitmapDrawable forShow = MapCachePool2.getInstance().getBitmapForShow(tile);
//            //在循环中就直接绘制了
//            if(null != forShow ){
//                tempList.add(forShow);
//            }
//        }
//    }
//
//    //放入屏幕外围的预加载任务
//            for (int y = tilesBound.minTileY-yMore; y <= tilesBound.maxTileY+yMore; y++) {
//        for (int x = tilesBound.minTileX-xMore; x <= tilesBound.maxTileX+xMore; x++) {
//            //接入外围区域
//            if(y>tilesBound.minTileY && y<tilesBound.maxTileY && x > tilesBound.minTileX && x < tilesBound.maxTileX ){
//                continue;
//            }
//            final MzTileBean tile = new MzTileBean(TIANDITU_SAT_KEY2, tilesBound.level, x, y);
//            drawTile(tile,cacheCanvas0);
////                    //外围区域不绘制，只预加载
//            MapCachePool2.getInstance().putForRequestTaskIfNeed(tile);
//        }
//    }

}
